const skinBehavior = require('../../../utils/themeSkin/skinBehavior');
import Dialog from '../../../dist/dialog/dialog';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatar:"",
    id:"",//兑换码记录id
    active: 0,
    show: false,
    code: "",
    dialog_type: 0,
    nickname:"",
    uid:"",
    name:"",
    show_tips:false,
    timer_id: null,
    keyword:"",
    code_sn:"",
    type:"binding",
    id:"",

    list: [],
    current_page: 1,
    last_page: 1,

    get_time_sort: 'asc',
    active_end_sort: 'asc',
    show_member_flag:false,
    member_list:[],

  },
  behaviors: [skinBehavior],

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({get_time_sort:'desc'})
    // this.getData({
    //   page: 1,order:{get_time:'desc'}
    // })
  },
  getData(json) {
    app._postNetWork({
      url: app.getNetAddresss('plugin.exchange-code.api.record.list'),
      data: json,
      success: resdata => {
        let {
          result,
          data: {
            data,
            current_page,
            last_page
          },
          msg
        } = resdata.data;
        if (result) {
          this.setData({
            current_page,
            last_page
          })

          if (current_page == 1) {
            this.setData({
              list: data || []
            })
          } else {
            this.setData({
              list: this.data.list.concat(data)
            })
          }
        } else {
          app.tips(msg)
        }
      }
    })
  },
  toHistory() {
    wx.navigateTo({
      url: '/packageI/redeem_code/history/history'
    })
  },
  showOverlay(e) {
    if(e.currentTarget.dataset.type == "1"){
      this.setData({
        type:"Donation",
        code_sn: e.currentTarget.dataset.datas.code_sn,
        code_id:e.currentTarget.dataset.datas.code_id,
        id:e.currentTarget.dataset.datas.id
      })
    }else{
      this.setData({binding:"binding"})
    }
    this.setData({
      show: true,
      dialog_type: e.currentTarget.dataset.type,
    })
  },
  link(e) {
    let {code_sn,code_id} = e.currentTarget.dataset.datas;
    wx.navigateTo({
      url: '/packageI/redeem_code/index/index?code_id='+code_id+'&sn_code='+code_sn ,
    })
  },
  HideOverlay() {
    this.setData({
      show: false
    })
  },
  input_handle(evt) {
    let type = evt.currentTarget.dataset.type;
    let value = evt.detail.value;
    if(type == 'code'){
      this.setData({code:value,type:'binding'})
    }else{
      this.setData({keyword:value,type:"presented"})
      if (this.data.timer_id) {
        clearTimeout(this.data.timer_id);
      }
      this.data.timer_id = setTimeout(() => {
        this.getMemberInfo(value)
      }, 600);
    }
  },
  confirm() {
    if (this.data.type == 'binding') {
      app._getNetWork({
        url:app.getNetAddresss('plugin.exchange-code.api.record.bind'),
        data:{
          sn:this.data.code,
        },
        success:resdata=>{
          let { result, msg }  = resdata.data;
          app.tips(msg);
          if(result){
            this.setData({show:false});
            let json = {};
            let index = this.data.active;
            if (index == 2 || index == 3) {
              json.search = {
                type: index - 1
              }
            }
            if (index == 0 || index == 1) {
              if (index == 0) {
                json.order = {
                  get_time: 'desc'
                };

              } else {
                json.order = {
                  active_end:'desc'
                };

              }
            }
            this.getData(json);
          }
        }
      })
    } else {
      Dialog.confirm({
        message:  '是否确认转赠给'+this.data.nickname,
      })
        .then(() => {
          app._getNetWork({
            url:app.getNetAddresss('plugin.exchange-code.api.record.tranfer'),
            data:{
              uid:this.data.uid,
              id:this.data.id
            },
            success:resdata=>{
              let { result, msg ,data:{title,imgUrl,desc}}  = resdata.data;
              app.tips(msg);
              if(result){
    
                wx.setStorageSync("wxShareInfo",{title,imgUrl,description:desc})
                setTimeout(()=>{
                  wx.navigateTo({
                    url: '/packageI/redeem_code/index/index?share=1&code_id='+this.data.code_id+'&sn_code='+this.data.code_sn,
                  })
                },300)
              }
            }
          })
        })
        .catch(() => {
          // on cancel
        });
    
    }
  },
  getMemberInfo(keyword) {
    app._getNetWork({
      url: app.getNetAddresss('plugin.exchange-code.api.record.memberSearch'),
      data: {
        keyword
      },
      success: resdata => {
        let {
          result,
          msg,
          data: {
            data
          }
        } = resdata.data;
        if (result) {
          if (data && data.length !== 0) {
            this.setData({
              member_list:data,
              show_member_flag:true,
              show_tips:false
            })
          }
          if (data.length == 0) {
            this.setData({show_tips:true,member_list:[],show_member_flag:false})
          }
        } else {
          app.tips(msg)
        }
      }
    })

  },
  choose_handle(e){
    let {uid,mobile,nickname,name,avatar} =  e.currentTarget.dataset.item;
    this.setData({
      avatar,
      uid,
      nickname,
      name,
      keyword:mobile||uid,
      show_member_flag:false
    })
  },
  onChange(event) {
    let index = event.detail.index;
    let json = this.get_tab_params(index);
    console.log(json,'json');
    this.getData(json);
    this.setData({
      active: event.detail.index
    })
  },
  get_tab_params(index) {
    let json = {};
    if (index == 2 || index == 3) {
      json.search = {
        type: index - 1
      }
    }
    if (index == 0 || index == 1) {
      if (index == 0) {

        if (this.data.get_time_sort == 'asc') {
          this.data.get_time_sort = 'desc'
        } else {
          this.data.get_time_sort = 'asc'
        }
        json.order = {
          get_time: this.data.get_time_sort
        };
      } else {

        if (this.data.active_end_sort == 'asc') {
          this.data.active_end_sort = 'desc'
        } else {
          this.data.active_end_sort = 'asc'
        }
        json.order = {
          active_end: this.data.active_end_sort
        };
      }
    }
    return json;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let json = {};
    let index = this.data.active;
    if (index == 2 || index == 3) {
      json.search = {
        type: index - 1
      }
    }
    if (index == 0 || index == 1) {
      if (index == 0) {
        json.order = {
          get_time: this.data.get_time_sort
        };
      } else {
        json.order = {
          active_end: this.data.active_end_sort
        };
      }
    }
    this.getData(json)
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.current_page >= this.data.last_page) {
      return;
    }
    let json = {};
    let index = this.data.active;
    if (index == 2 || index == 3) {
      json.search = {
        type: index - 1
      }
    }
    if (index == 0 || index == 1) {
      if (index == 0) {
        json.order = {
          get_time: this.data.get_time_sort
        };
      } else {
        json.order = {
          active_end: this.data.active_end_sort
        };
      }
    }
    this.getData({
      page: this.data.current_page + 1,
      ...json
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})